package sms;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

public class SMSUtil {
    //区域ID
    private static final String REGION_ID="cn-hangzhou";
    //accessKeyId
    private static final String ACCESS_KEY_ID="LTAI5tLwAgq46J5UzFvy8YKT";
    //accessKeyId secret
    private static final String ACCESS_KEY_SECRET="2NpPlyFDIIEIJij04h4yEI79nS9qzQ";
    //签名名称
    private static final String SIGN_NAME="焦金喜的博客";
    //短信模板Code
    private static final String TEMPLATE_CODE="SMS_461986294";

    public static boolean send(String phone,String code){
        DefaultProfile profile =
                DefaultProfile.getProfile(REGION_ID,
                        ACCESS_KEY_ID,
                        ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        //接收短信的手机号码
        request.setPhoneNumbers(phone);
        //短信签名名称
        request.setSignName(SIGN_NAME);
        //短信模板CODE
        request.setTemplateCode(TEMPLATE_CODE);
        //code阿里云模板只有一个短信参数
        request.putQueryParameter("TemplateParam","{'code':'"+code+"'}");
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println("发送短信响应信息："+new Gson().toJson(response));
            if(response.getCode().equals("OK")){
                return true;
            }
        } catch (ClientException e) {
           e.printStackTrace();

        }
        return false;
    }

    public static void main(String[] args) {
        boolean flag=send("15701394542","6666");
        if(flag){
            System.out.println("发送成功");
        }else{
            System.out.println("发送失败");
        }
    }

}
